﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReadCSV;
namespace Form2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        DanhSach obj;

        private void Form1_Load(object sender, EventArgs e)
        {
            labShow.Text = ThongTin();
        }

        void loadData(string url)
        {
            obj = new DanhSach(url);
            loadData();             
        }

        void loadData()
        {
            listData.Clear();
            int n = obj.SoThuocTinh;
            int m = (listData.Width - 33) / n;

            listData.Columns.Add("STT", 33);

            for (int i = 0; i < n; i++)
                listData.Columns.Add(obj.danhsach[i].TenThuocTinh, m);

            for (int i = 0; i < obj.SoLuongMau; i++)
            {
                ListViewItem listtemp;
                string[] a = new string[n + 1];
                a[0] = (i + 1).ToString();
                for (int j = 0; j < n; j++)
                {
                    a[j + 1] = obj.danhsach[j].DanhSach[i];
                }
                listtemp = new ListViewItem(a);
                listData.Items.Add(listtemp);
            }        
        }

        void loadDanhSach()
        {
            listBoxdanhsach.Items.Clear();
            if (obj.danhsach != null)
            {
                for (int i = 0; i < obj.SoThuocTinh; i++)
                    listBoxdanhsach.Items.Add(obj.danhsach[i].TenThuocTinh);
            }
        }

        private void butChon_Click(object sender, EventArgs e)
        {
            string temp = "";
            OpenFileDialog ofi = new OpenFileDialog();
            ofi.Filter = "CSV File|*.csv";
            if (ofi.ShowDialog() == DialogResult.OK)
            {
                temp = ofi.FileName;
                textURL.Text = temp;
                loadData(temp);
                loadDanhSach();
                texttongdulieu.Text = obj.SoLuongMau.ToString();                
                Show();
                but_luuChiaGio.Enabled = true;
            }            
        }

        void Show_ThongTin_TungThuocTinh(int t)
        {
            labThongtin.Text = ThongTin(t);
            groupTT.Text = listBoxdanhsach.Items[t].ToString();
            DanhSachPhanBiet(t);
            RadioChon(t);        
        }

        private void comdanhsach_SelectedIndexChanged(object sender, EventArgs e)
        {
            int t = listBoxdanhsach.SelectedIndex;
            if (t != -1)
            {
                Show_ThongTin_TungThuocTinh(t);
            }

        }

        void DanhSachPhanBiet(int vt)
        {
            listDanhSachPhanBiet.Items.Clear();
            for (int i = 0; i < obj.danhsach[vt].DanhSachMauPhanBiet.Count; i++)
            {
                string[] temp = new string[2];
                temp[0] = obj.danhsach[vt].DanhSachMauPhanBiet[i];
                temp[1] = obj.danhsach[vt].DemPhanBiet[i].ToString();
                ListViewItem t = new ListViewItem(temp);
                listDanhSachPhanBiet.Items.Add(t);
            }
        }

        string ThongTin()
        {
            string temp = "\n";
            temp += "Loại thuộc tính: \n";
            temp += "Số lượng mẫu: \n";
            temp += "Min: \n";
            temp += "Max: \n";
            temp += "Giá trị đại diện: \n";
            temp += "Số giá trị phân biệt: \n";
            temp += "Số giá trị bị thiếu: \n";
            temp += "Mean: \n";
            temp += "StdDev: ";
            return temp;
        }

        string ThongTin(int vt)
        {
            string temp = "\n";
            temp += obj.danhsach[vt].LoaiThuocTinh + "\n";
            temp += obj.SoLuongMau + "\n";
            temp += obj.danhsach[vt].Min + "\n";
            temp += obj.danhsach[vt].Max + "\n";
            temp += obj.danhsach[vt].GiaTri + "\n";
            temp += obj.danhsach[vt].SoMauPhanBiet + "\n";
            temp += obj.danhsach[vt].SoMauThieu + "\n";
            temp += obj.danhsach[vt].mean + "\n";
            temp += obj.danhsach[vt].StdDev + "\n";
            return temp;
        }

        void RadioChon(int t)
        {
            int vt = listBoxdanhsach.SelectedIndex;
            if (vt != -1 && groupBox_chiagio.Enabled)
            {
                if (radioRong.Checked == false && radioSau.Checked == false)
                    radioRong.Checked = true;
                if (!obj.Miss)
                {
                    if (radioRong.Checked)
                        xuly_chiagio_chieurong(vt);
                    else
                        xuly_chiagio_chieusau(vt);
                }
            }
        }

        void xuly_chiagio_chieurong(int t)
        {
            listChiaGio.Items.Clear();            
            if (textTiLe.Text != "" && obj.danhsach[t].LoaiThuocTinh=="numeric" && !obj.Miss)
            {
                try
                {
                    int n = Convert.ToInt32(textTiLe.Text);                    
                    List<string> ds = new List<string>();
                    List<int> ts = new List<int>();

                    obj.ChiaChieuRong(t, n, ref ds, ref ts);

                    listChiaGio.Items.Clear();
                    for (int i = 0; i < ds.Count; i++)
                    {
                        string[] temp = new string[2];
                        temp[0] = ds[i];
                        temp[1] = ts[i].ToString();
                        var te = new ListViewItem(temp);
                        listChiaGio.Items.Add(te);
                    }
                    ds.Clear();
                    ts.Clear();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi chia theo chiều rộng \n"+ex.Message);
                }
            }
        }

        void xuly_chiagio_chieusau(int t)
        {
            if (textTiLe.Text != "" && obj.danhsach[t].LoaiThuocTinh == "numeric" && !obj.Miss)
            {
                try
                {
                    int n = Convert.ToInt32(textTiLe.Text);

                    List<string> ds = new List<string>();
                    List<int> ts = new List<int>();

                    obj.ChiaChieuSau(t, n, ref ds,ref ts);

                    listChiaGio.Items.Clear();
                    for (int i = 0; i < ds.Count; i++)
                    {

                        string[] temp = new string[2];
                        temp[0] = ds[i];
                        temp[1] = ts[i].ToString();
                        var te = new ListViewItem(temp);
                        listChiaGio.Items.Add(te);
                    }
                    ds.Clear();
                    ts.Clear();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi chia theo chiều sâu \n"+ex.Message+ex);
                }
            }
        }

        void Show()
        {
            listBoxdanhsach.SelectedIndex = 0;
            labThongtin.Text = "_";
            listDanhSachPhanBiet.Items.Clear();
            listChiaGio.Items.Clear();
            RadioChon(0);
            Show_ThongTin_TungThuocTinh(0);            
            
            if (!obj.Miss)
            {
                groupBox_chuanhoa.Enabled = true;
                groupBox_chiagio.Enabled = true;
                but_replacemissvalue.Enabled = false;              
            }
            else
            {
                groupBox_chuanhoa.Enabled = false;
                groupBox_chiagio.Enabled = false;
                but_replacemissvalue.Enabled = true;
            }
            if (!but_luuChiaGio.Enabled)
            {
                groupBox_chuanhoa.Enabled = false;
                groupBox_chiagio.Enabled = false;
            }
            if (radioRong.Checked == false && radioSau.Checked == false)
                radioRong.Checked = true;
        }

        private void radioRong_CheckedChanged(object sender, EventArgs e)
        {
            if (radioRong.Checked)
            {
                int t = listBoxdanhsach.SelectedIndex;
                xuly_chiagio_chieurong(t);
            }
        }

        private void radioSau_CheckedChanged(object sender, EventArgs e)
        {
            if (radioSau.Checked)
            {
                int t = listBoxdanhsach.SelectedIndex;
                xuly_chiagio_chieusau(t);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            obj.replace();
            obj.Load();
            loadData();
            Show();            
        }

        private void but_chuan_Minmax_Click(object sender, EventArgs e)
        {
            try
            {
                double min = Convert.ToDouble(text_newmin.Text);
                double max = Convert.ToDouble(text_newmax.Text);
                obj.ChuanMinMax(min, max);                
                obj.Load();                
                loadData();                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nhập lại giá trị new Min - new Max + \n"+ex.Message+ex.Source);
            }
            
        }

        private void but_Chuan_Zscore_Click(object sender, EventArgs e)
        {
            try
            {
                obj.ChuanZScore();                
                obj.Load();                
                loadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi chuẩn hóa Z-score + \n" + ex.Message);
            }
        }

        private void button_chiagio_Click(object sender, EventArgs e)
        {
            int t = listBoxdanhsach.SelectedIndex;
            if (groupBox_chiagio.Enabled)
            {
                if (radioRong.Checked)
                    xuly_chiagio_chieurong(t);
                else
                    xuly_chiagio_chieusau(t);
            }
        }

        private void but_luuChiaGio_Click(object sender, EventArgs e)
        {
            try
            {
                int t = Convert.ToInt32(textTiLe.Text);
                if (radioRong.Checked)
                {
                    obj.ChiaGio(t, 0);
                }
                else
                {
                    obj.ChiaGio(t, 1);
                }
                obj.Load();
                loadData();
                but_luuChiaGio.Enabled = false;
                Show();                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại số giỏ \n" + ex.Message);
            }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "CSV File|*.csv|ARFF File|*.arff";
            if (save.ShowDialog() == DialogResult.OK)
            {
                string filename = save.FileName;
                int t = save.FilterIndex;
                obj.SaveFile(filename, t);
            }
        }

        private void listChiaGio_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}
